package com.yyq.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyq.eduservice.entity.EduChapter;
import com.yyq.eduservice.entity.EduVideo;
import com.yyq.eduservice.entity.chapter.chapter_1;
import com.yyq.eduservice.entity.chapter.vedio_2;
import com.yyq.eduservice.mapper.EduChapterMapper;
import com.yyq.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyq.eduservice.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author yyq
 * @since 2021-04-15
 *
 * 别忘记ＩＤ随机生成的　一级表示随机生成　　二级表　ｉｄ随机生成　
 * 所以每个表的ｉｄ不对应
 *
 * 方法一
 * Ｃｈａｐｔｅｒ　　　　　　ｖｅｄｉｏ
 * ｉｄ　－－－随机生成的　　ｉｄ　　－－－随机生成的
* ｃｏｕｒｓｅ＿ｉｄ　　　　　ｃｏｕｒｓｅ＿ｉｄ
 * 　　　　　　　　　　　　　ｃｈａｐｅｔｅｒ＿ｉｄ　　　－－－－－对应章节的ｉｄ
 *
 *
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService eduVideoService;
    @Override
    public List<chapter_1> getlist(String course_id) {
        List<chapter_1> list =new ArrayList<>();

        //首先获取ｃｈａｐｔｅｒ课程对应的具体章节
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",course_id);
        //得到所有的章节信息都在这里面
        List<EduChapter> chaterList = baseMapper.selectList(wrapper);

        //查询ｃｏｕｒｓｅｉｄ　对应的所有小结
        QueryWrapper<EduVideo> wrapper1=new QueryWrapper<>();
        //注入ｓｅｒｖｉｃｅ
        //得到所有的小结信息
        List<EduVideo> videoList = eduVideoService.list(wrapper1);
        //往Ｌｉｓｔ中加入章节信息
        for(int i=0;i<chaterList.size();i++){
            //创建一个ｃｈａｐｔｅｒ＿１用来装入数据
            chapter_1 chapter_1=new chapter_1();
            BeanUtils.copyProperties(chaterList.get(i),chapter_1);

            //接下来装入小结信息
            //创建一个ｖｅｄｉｏ＿２用来装入查询返回数据
            List<vedio_2> vedio_2List=new ArrayList<>();
            for (int j=0;j<videoList.size();j++){
                if(chapter_1.getId().equals(videoList.get(j).getChapterId())) {
                    vedio_2 vedio_2=new vedio_2();
                    BeanUtils.copyProperties(videoList.get(j),vedio_2);
                    vedio_2List.add(vedio_2);
                }
            }
            chapter_1.setChildren(vedio_2List);
            list.add(chapter_1);
        }


        return list;
    }

    @Override
    public String deleteChapter(String chapter_id) {
        //查询小结信息
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id",chapter_id);
        int count = eduVideoService.count(wrapper);
        //不能删除　有小结记录
        if(count>0){
            return "0";
        }
        //删除章节
        else {
            int i = baseMapper.deleteById(chapter_id);
            return "1";
        }

    }

    @Override
    public void deleteByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
